package com.zhongxi.guoyuan.adapter.seller;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.seller.SellerBeautifulOrderData;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.DataUtil;

import org.json.JSONObject;

import java.util.List;

import static com.zhongxi.guoyuan.R.id.tv_goodsCount;

/**
 * Created by Administrator on 2016/12/21.
 */

public class SellerBeautifulAdapter extends BaseAdapter {

    private Context context;
    private List<SellerBeautifulOrderData> list;
    private int type;
    private Handler handler;
    private JSONObject user;
    private JSONObject shopInfo;
    private JSONObject complaints;

    public SellerBeautifulAdapter(Context context, List<SellerBeautifulOrderData> orderData, Handler handler) {
        this.context = context;
        this.list = orderData;
        this.handler = handler;
    }

    public void setType(int type) {
        this.type = type;
    }

    @Override
    public int getCount() {
        if (list != null && list.size() > 0) {
            return list.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        try {
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.item_seller_beautiful_order, parent, false);
                holder.ivImg = (ImageView) convertView.findViewById(R.id.ivImg);
                holder.tv_clock = (TextView) convertView.findViewById(R.id.tv_clock);

                holder.tvServiceName = (TextView) convertView.findViewById(R.id.tvServiceName);
                holder.tvServicePrice = (TextView) convertView.findViewById(R.id.tvServicePrice);
                holder.tvHandWorkPrice = (TextView) convertView.findViewById(R.id.tvHandWorkPrice);
                holder.tvHD = (TextView) convertView.findViewById(R.id.tvHD);
                holder.tvClientPhone = (TextView) convertView.findViewById(R.id.tvClientPhone);

                holder.tvOper = (TextView) convertView.findViewById(R.id.tvOper);
                holder.tv_call = (TextView) convertView.findViewById(R.id.tv_call);

                holder.ll_complain = (LinearLayout) convertView.findViewById(R.id.ll_complain);

                holder.tv_complain_time = (TextView) convertView.findViewById(R.id.tv_complain_time);
                holder.tv_complain = (TextView) convertView.findViewById(R.id.tv_complain);
                holder.tv_complain_tip = (TextView) convertView.findViewById(R.id.tv_complain_tip);

                holder.tv_goodsCount = (TextView) convertView.findViewById(tv_goodsCount);
                holder.tv_orderPhone = (TextView) convertView.findViewById(R.id.tv_orderPhone);

                holder.tvUserName = (TextView) convertView.findViewById(R.id.tvUserName);
                holder.tvPhone = (TextView) convertView.findViewById(R.id.tvPhone);
                holder.tvAddress = (TextView) convertView.findViewById(R.id.tvAddress);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            final SellerBeautifulOrderData data = list.get(position);
            final int pos = position;
            final JSONObject userInfo = user.optJSONObject(data.getUserid());
            if (userInfo != null && !"".equals(userInfo)) {
                final String mphone = userInfo.optString("mphone");
                holder.tvClientPhone.setText("顾客：" + mphone);
                holder.tv_call.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(Intent.ACTION_DIAL);
                        Uri data = Uri.parse("tel:" + mphone);
                        intent.setData(data);
                        context.startActivity(intent);
                    }
                });
            } else {
                holder.tvClientPhone.setText("手机号：(null)");
            }
            if (null != data.getPaytime() && !"".equals(data.getPaytime())) {
                holder.tv_orderPhone.setText("支付时间：" + data.getPaytime());
            } else {
                holder.tv_orderPhone.setText("");
            }
            switch (type) {   //待确认
                case 1:
                    holder.ll_complain.setVisibility(View.GONE);
                    holder.tv_complain.setVisibility(View.GONE);
                    holder.tv_complain_time.setVisibility(View.GONE);
                    holder.tv_complain_tip.setVisibility(View.GONE);

                    holder.tvOper.setVisibility(View.VISIBLE);
                    holder.tvOper.setText(" 受 理 ");
                    holder.tvOper.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            AlertDialog dialog = new AlertDialog.Builder(context).setTitle("您确认执行此操作吗？")
                                    .setPositiveButton("确认执行", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            Message msg = Message.obtain();
                                            Bundle bundle = new Bundle();
                                            bundle.putInt("pos", pos);
                                            bundle.putString("orderid", data.getId());
                                            bundle.putString("sid", data.getItemid());
                                            msg.setData(bundle);
                                            msg.what = 0;
                                            msg.setData(bundle);
                                            handler.sendMessage(msg);
                                            dialog.dismiss();
                                        }
                                    }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            dialog.dismiss();
                                        }
                                    }).show();
                            dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(context.getResources().getColor(R.color.bg_yield));
                            dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(context.getResources().getColor(R.color.bg_yield));
                        }
                    });
                    break;
                case 2:   //待操作
                    holder.ll_complain.setVisibility(View.GONE);
                    holder.tv_complain.setVisibility(View.GONE);
                    holder.tv_complain_time.setVisibility(View.GONE);
                    holder.tv_complain_tip.setVisibility(View.GONE);
                    holder.tvOper.setVisibility(View.VISIBLE);
                    holder.tvOper.setText("确认操作");
                    holder.tvOper.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            AlertDialog dialog = new AlertDialog.Builder(context).setTitle("您确认该项目已经护理结束吗？")
                                    .setPositiveButton("确定已结束", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            Message msg = Message.obtain();
                                            Bundle bundle = new Bundle();
                                            bundle.putInt("pos", pos);
                                            bundle.putString("orderid", data.getId());
                                            bundle.putString("sid", data.getItemid());
                                            msg.setData(bundle);
                                            msg.what = 1;
                                            msg.setData(bundle);
                                            handler.sendMessage(msg);
                                            dialog.dismiss();
                                        }
                                    }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            dialog.dismiss();
                                        }
                                    }).show();
                            dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(context.getResources().getColor(R.color.bg_yield));
                            dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(context.getResources().getColor(R.color.bg_yield));
                        }
                    });
                    break;
                case 3:   //已投诉
                    holder.ll_complain.setVisibility(View.VISIBLE);
                    holder.tv_complain.setVisibility(View.VISIBLE);
                    holder.tv_complain_time.setVisibility(View.VISIBLE);
                    holder.tv_complain_tip.setVisibility(View.VISIBLE);
                    holder.tvOper.setVisibility(View.GONE);
                    break;
            }
            if (null != data.getId() && !"".equals(data.getId())) {
                holder.tv_goodsCount.setText("订单编号：" + data.getId());
            } else {
                holder.tv_goodsCount.setText("");
            }

            if (null != data.getServicetime() && !"".equals(data.getServicetime())) {
                holder.tv_clock.setText(data.getServicetime() + "分钟");
            } else {
                holder.tv_clock.setText("");
            }
            if (null != data.getThumbnail() && !"".equals(data.getThumbnail())) {
                Glide.with(context).load(Url.IMG_URL + data.getThumbnail()).placeholder(R.mipmap.iv_eg).into(holder.ivImg);
            }
            if (null != data.getItemname() && !"".equals(data.getItemname())) {
                holder.tvServiceName.setText(data.getItemname());
            } else {
                holder.tvServiceName.setText("");
            }

            if (null != data.getInnerprice() && !"".equals(data.getInnerprice())) {
                holder.tvServicePrice.setText(data.getInnerprice() + "美豆");
            } else {
                holder.tvServicePrice.setText("");
            }

            if (DataUtil.moreThanZero(data.getPrice()) && DataUtil.moreThanZero(data.getRedbeanprice())) {
                holder.tvHandWorkPrice.setText(data.getPrice() + " 元");
                holder.tvHD.setText("+" + data.getRedbeanprice() + " 红豆");
            } else if (!DataUtil.moreThanZero(data.getPrice()) && DataUtil.moreThanZero(data.getRedbeanprice())) {
                holder.tvHandWorkPrice.setText(data.getRedbeanprice() + " 红豆");
                holder.tvHandWorkPrice.setTextColor(context.getResources().getColor(R.color.title_red));
                holder.tvHD.setText("");
            } else {
                holder.tvHandWorkPrice.setText(data.getPrice() + " 元");
                holder.tvHD.setText("");
            }

            if (null != shopInfo && !"".equals(shopInfo)) {
                JSONObject jsonObject = shopInfo.optJSONObject(data.getShopid());
                if (null != jsonObject && !"".equals(jsonObject)) {
                    String name = jsonObject.optString("name");
                    holder.tvUserName.setText(name);
                    String mphone = jsonObject.optString("tel");
                    if (null != mphone && !"".equals(mphone)) {
                        holder.tvPhone.setText(mphone);
                    } else {
                        String tel = jsonObject.optString("mphone");
                        holder.tvPhone.setText(tel);
                    }
                    String address = jsonObject.optString("address");
                    holder.tvAddress.setText(address);
                }
            }

            if (null != complaints && !"".equals(complaints)) {
                String s = complaints.optString(data.getId());

                holder.tv_complain.setText(s);
                if (null != data.getUsercomplaintime() && !"".equals(data.getUsercomplaintime())) {
                    holder.tv_complain_time.setText(data.getUsercomplaintime());
                } else {
                    holder.tv_complain_time.setText("");
                }
            } else {
                holder.ll_complain.setVisibility(View.GONE);
                holder.tv_complain.setVisibility(View.GONE);
                holder.tv_complain_time.setVisibility(View.GONE);
                holder.tv_complain_tip.setVisibility(View.GONE);

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }

    public void setJSONObject(JSONObject user) {
        this.user = user;
    }

    public void setShopInfo(JSONObject shopInfo) {
        this.shopInfo = shopInfo;
    }

    public void setComplaints(JSONObject complaints) {
        this.complaints = complaints;
    }

    class ViewHolder {
        private ImageView ivImg;
        private TextView tv_clock;
        private TextView tvServiceName;
        private TextView tvServicePrice;
        private TextView tvHandWorkPrice;
        private TextView tvHD;
        private TextView tvClientPhone;
        private TextView tvOper;
        private TextView tv_call;
        private LinearLayout ll_complain;
        private TextView tv_complain_time;
        private TextView tv_complain;
        private TextView tv_goodsCount;
        private TextView tv_orderPhone;
        private TextView tvUserName;
        private TextView tvPhone;
        private TextView tvAddress;
        private TextView tv_complain_tip;
    }
}
